<template>
	<view class="login">
		<view class="container">
			<image src="https://dingsheng-taiqiu.oss-cn-shenzhen.aliyuncs.com/20230203/1675391336497299.png"
				class="logo"></image>
			<view class="loginFooter">
				<view class="agreement">
					<checkbox value="consent" checked="true" color="#E60012" style="transform:scale(0.7)" />
					<text>我已阅读并同意《隐私条款》</text>
				</view>
				<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		authApplets,
		authPhone,
		getUserDetail
	} from "@/api/getData.js";
	export default {
		data() {
			return {
				consent: true,
				fromPath: null,
				decodePhoneParams: {
					code: null,
					encryptedData: null,
					iv: null,
					sessionKey: null,
					openId: null
				},
			}
		},
		onLoad(options) {
			this.getCode()
			console.log(getApp().globalData.fromPath,3333)
		},
		methods: {

			//获取用户code
			getCode() {
				var that = this
				uni.login({
					provider: 'weixin',
					success: res => {
						that.userAuth(res.code)
					}
				});
			},

			//用户code授权
			async userAuth(code) {
				const {
					data: res
				} = await authApplets(code)
				if (res.data.token) {
					this.$store.commit('SET_TOKEN', res.data.token);
					const setData = {}
					setData.nickName = res.data.nickName
					setData.gender = res.data.gender
					setData.imgUrl = res.data.imgUrl
					this.$store.commit('SET_USERINFO', setData);
					uni.redirectTo({
						url: getApp().globalData.fromPath
					})
				} else {
					this.decodePhoneParams.sessionKey = res.data.sessionKey
					this.decodePhoneParams.openId = res.data.openId
					// this.userInfoParams.openId = res.data.openId
				}
			},
			// 获取手机号
			getPhoneNumber(e) {
				console.log(e)
				this.decodePhoneParams.code = e.detail.code
				this.decodePhoneParams.encryptedData = e.detail.encryptedData
				this.decodePhoneParams.iv = e.detail.iv
				this.authPhone()
			},
			//用户手机号授权
			async authPhone() {
				const {
					data: res
				} = await authPhone(this.decodePhoneParams)
				if (res.data.token) {
					this.$store.commit('SET_TOKEN', res.data.token);
					const setData = {}
					setData.nickName = res.data.nickName
					setData.gender = res.data.gender
					setData.imgUrl = res.data.imgUrl
					this.$store.commit('SET_USERINFO', setData);
					uni.$showMsg('授权成功', 1500, 'success')
					uni.redirectTo({
						url: getApp().globalData.fromPath
					})
				} else {}
			}
		}
	}
</script>

<style lang="less">
	.login {
		.container {
			padding: 32rpx;

			.logo {
				display: block;
				width: 400rpx;
				height: 400rpx;
				margin: 300rpx auto;
			}

			.loginFooter {
				position: fixed;
				width: 100%;
				left: 0;
				bottom: 0;
				box-sizing: border-box;
				padding: 0 50rpx;

				.agreement {
					text {
						font-size: 24rpx;
					}
				}

				button {
					width: 100%;
					height: 98rpx;
					background: #E60012;
					border-radius: 49rpx;
					font-size: 32rpx;
					font-weight: bold;
					color: #FFFFFF;
					line-height: 98rpx;
					padding: 0;
					margin-top: 12rpx;
					margin-bottom: 40rpx;
				}
			}
		}
	}
</style>
